package week_six;

import java.util.Arrays;

public class BestTimeToBuySellStockWithCooldown_309 {
    public int maxProfit(int[] prices) {

        int Min_Value = (int) -1e9;

        int n = prices.length;
        int[] newPrices = new int[n + 1];
        for (int i = 1; i <= n; i++) newPrices[i] = prices[i - 1];

        int[][][] f = new int[n + 1][2][2];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(f[i][0], Min_Value);
            Arrays.fill(f[i][1], Min_Value);
        }
        f[0][0][0] = 0;

        for (int i = 0; i < n; i++)
            for (int j = 0; j <= 1; j++)
                for (int l = 0; l <= 1; l++) {
                    if (j == 0 && l == 0)
                        f[i + 1][1][0] = Math.max(f[i + 1][1][0], f[i][j][l] - newPrices[i + 1]);
                    if (j == 1 && l == 0)
                        f[i + 1][0][1] = Math.max(f[i + 1][0][1], f[i][j][l] + newPrices[i + 1]);
                    f[i + 1][j][0] = Math.max(f[i + 1][j][0], f[i][j][l]);
                }

        int ans = 0;
        for (int l = 0; l <= 1; l++) ans = Math.max(ans, f[n][0][l]);
        return ans;

    }
}
